import numpy as np
import csv
from sklearn.decomposition import PCA,KernelPCA,FastICA,RandomizedPCA
from sklearn.ensemble import ExtraTreesClassifier,RandomForestClassifier,GradientBoostingClassifier
from sklearn.svm import SVC,LinearSVC
from sklearn import preprocessing,grid_search
import sys
from sklearn.multiclass import OneVsRestClassifier
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import f1_score,average_precision_score
from sklearn.neural_network import BernoulliRBM
from sklearn.cross_decomposition import CCA

def load_data_csv(file):
	lines = csv.reader(open(file))
	data = []
	for line in lines:
		data.append(line)
	data=np.asarray(data)
	return data

data = load_data_csv('adult/train_data.csv')

X=data[:,:129].astype(float)
Y=data[:,129:].astype(int)

XX=[]
YY=[]
size=12
print "shape of X: ", X.shape
print "shape of Y: ", Y.shape
for x,y in zip(X,Y):
	#print len(y)
	#print sum(y)
	for i in range(size):
		if y[i] == 1:
			XX.append(x)
			YY.append(i)

X=np.asarray(XX)
Y=np.asarray(YY)
print "shape of X: ", X.shape
print "shape of Y: ", Y.shape

test = load_data_csv('task1/test_feature_data.csv')
test = test[:,1:].astype(float)
print "shape of test: ",test.shape

pca = RandomizedPCA(n_components=88) #best: 56.13, n_components=110, PCA
#pca = FastICA(n_components=)
pca.fit(X)
X=pca.transform(X)
test=pca.transform(test)

#Y = Y.astype(int)
#clf=OneVsRestClassifier(SVC(kernel="rbf",probability=True))#SVC(C=1.0,probability=True,gamma=0)
#clf=OneVsRestClassifier(LinearSVC(class_weight='auto'))
clf=SVC(probability=True)
clf.fit(X, Y)
ret=clf.predict_proba(test)

'''
model_to_set = OneVsRestClassifier(SVC(kernel="poly",probability=True))
#model_to_set = SVC(kernel="poly",probability=True)
parameters = {
    "estimator__C": [1,2,4,8],
    "estimator__kernel": ["poly","rbf"],
    "estimator__degree":[1, 2, 3, 4],
}
clf = GridSearchCV(model_to_set, param_grid=parameters,
                             score_func=average_precision_score)
'''

'''
for i in range(20):
	pca = PCA(n_components=100+1*i)
	pca.fit(X)
	xx=pca.transform(X)
	tt=pca.transform(test)
	clf=SVC(probability=True)
	clf.fit(xx, Y)
	r=clf.predict_proba(tt)
	if i == 0:
		ret = r
	else:
		ret = ret + r
ret = ret / 20.
'''


times=10
for i in range(times):
	#clf=SVC(C=2.0/(times%10+1),probability=True,random_state=i)
	clf=SVC(C=2.0,probability=True,random_state=i)
	clf.fit(X, Y)
	r=clf.predict_proba(test)
	if i == 0:
		ret = r
	else:
		ret = ret + r
ret = ret / 10.

ret=np.transpose(np.asarray(ret))
print ret

'''
pca = PCA(n_components=200)
pca.fit(X)
xx=pca.transform(X)
tt=pca.transform(test)
'''
print len(ret)
print len(ret[0])

f = open("task1_pre_multi.csv", "w")

for k in range(204):
	#print >>f,"%d"%(k+1),
	f.write('%d' %(k+1))
	for i in range(size):
		#r=ret[i]
		#print len(r)
		f.write(',%.6f' %(ret[i][k]) )
		#print >> f,"\b,%.2f" %(ret[i][k]),
	#print >> f
	f.write('\n')
f.close()





